<?php

/**
 * @copyright   FOSP
 * @version     20120409
 * @todo        PlayerTeamMatch model
 * @name        PlayerTeamMatch
 * @author      QuangTM 
 */
class PlayerTeamMatch
{

    const PLAYER_TEAM_MATCH = 'player_team_match_list';

    /**
     *
     * @var PlayerTeamMatch
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get PlayerTeamMatch's instance
     * @return PlayerTeamMatch
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Get list players by team and match
     * @param int $matchID
     * @param int $teamID
     * @return array|boolean 
     * @author QuangTM
     */
    public function getPlayersTeamMatch($matchID, $teamID)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($teamID);
        $keyCache         = Fpt_Global::makeCacheKey(self::PLAYER_TEAM_MATCH, array($matchID, $teamID));

        // Read cache
        $arrPlayersTeamMatch = $memcacheInstance->read($keyCache);

        // Miss cache
        if ($arrPlayersTeamMatch === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();

                // Get db teamseason instance
                $playerTeamMatchObj = Fpt_Business_PlayerTeamMatch::factory($config['database']['default']);

                // Get data from db
                $arrPlayersTeamMatch = $playerTeamMatchObj->getPlayersTeamMatch($matchID, $teamID);

                // Write to cache
                if (count($arrPlayersTeamMatch))
                {
                    Fpt_Global::writeMemcache($matchID, $keyCache, $arrPlayersTeamMatch);
                }
                else
                {
                    Fpt_Global::writeMemcache($matchID, $keyCache, -1);
                }
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        else if ($arrPlayersTeamMatch == -1)
        {
            return array();
        }

        return $arrPlayersTeamMatch;
    }

}